package com.jukte.recipe.adapters;

import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ewhat.recipes.R;
import com.jukte.recipe.models.Recipe;

public class RecipeListAdapter extends ArrayAdapter<Recipe>{

	private final Activity context;
	private List<Recipe> items;
	//private  Site temp;
	
	static class ViewHolder {
	    public TextView nameText;
	    public ImageView thumbImage;
	    public View vegetImage;
	  }
	
	public RecipeListAdapter(Activity context,
			int textViewResourceId, List<Recipe> objects) {
		super(context,  textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context=context;
		items=objects;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		View rowView = convertView;
	    if (rowView == null) {
	      LayoutInflater inflater = context.getLayoutInflater();
	      rowView = inflater.inflate(R.layout.row_recipelist, null);
	      ViewHolder viewHolder = new ViewHolder();
	      viewHolder.nameText = (TextView) rowView.findViewById(R.id.toptext);
	      viewHolder.thumbImage=(ImageView)rowView.findViewById(R.id.rowimage);
	      viewHolder.vegetImage= rowView.findViewById(R.id.vegetImage);
	      rowView.setTag(viewHolder);
	    }

	    ViewHolder holder = (ViewHolder) rowView.getTag();
	    final Recipe temp=items.get(position);
	    holder.nameText.setText(temp.getName());
	    
	    int resId = context.getResources().getIdentifier("quinoa_menuicon","raw", context.getPackageName());
		if(temp.getImgPathThumnail()!=null){
			Bitmap bmp = BitmapFactory.decodeFile(context.getFilesDir().getPath()+"/raw/"+temp.getImgPathThumnail());
			if(null != bmp){
				holder.thumbImage.setImageBitmap(bmp);
			}else{
				Log.i("XXXXXXX", temp.getName() + " => " +temp.getImgPathThumnail());
				holder.thumbImage.setImageResource(resId);
			}
		}
		else{
			Log.i("XXXXXXX", temp.getId()+"");
			holder.thumbImage.setImageResource(resId);
		}
		
		if(temp.isVegetarian()){
			holder.vegetImage.setVisibility(View.VISIBLE);
		}else{
			holder.vegetImage.setVisibility(View.GONE);
		}
		
	    return rowView;
	  }

	
	}

	


